package cn.git.shiro.result;

import cn.git.shiro.enums.ResultEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @description: 响应结果
 * @program: bank-credit-sy
 * @author: lixuchun
 * @create: 2024-01-05 10:18:16
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> {

    /**
     * 响应结果
     */
    private T result;

    /**
     * 是否成功
     * true/false
     */
    private boolean issuccess;

    /**
     * 响应编码
     * 0000 成功
     */
    private String rtncode;

    /**
     * 响应消息
     * 交易成功/交易失败
     */
    private String rtnmessage;

    /**
     * 支持信息
     * 请联系服务商
     */
    private String solution;

    /**
     * 构造函数
     */
    private Result(ResultEnum ResultEnum) {
        this.issuccess = ResultEnum.isIssuccess();
        this.rtncode = ResultEnum.getRtncode();
        this.rtnmessage = ResultEnum.getRtnmessage();
        this.solution = ResultEnum.getSolution();
    }

    /**
     * 构造函数
     */
    private Result(ResultEnum ResultEnum, T data) {
        this.result = data;
        this.issuccess = ResultEnum.isIssuccess();
        this.rtncode = ResultEnum.getRtncode();
        this.rtnmessage = ResultEnum.getRtnmessage();
        this.solution = ResultEnum.getSolution();
    }

    /**
     * 构造函数
     */
    private Result(ResultEnum ResultEnum, String message) {
        this.result = null;
        this.issuccess = ResultEnum.isIssuccess();
        this.rtncode = ResultEnum.getRtncode();
        this.rtnmessage = message;
        this.solution = ResultEnum.getSolution();
    }

    /**
     * 构造函数
     */
    private Result(ResultEnum ResultEnum, String message, String rtncode) {
        this.result = null;
        this.issuccess = ResultEnum.isIssuccess();
        this.rtncode = ResultEnum.getRtncode();
        this.rtnmessage = message;
        this.solution = ResultEnum.getSolution();
    }

    /**
     * 构造函数
     * @param result 响应结果
     * @param issuccess 是否成功
     * @param rtncode 响应编码
     * @param rtnmessage 响应消息
     */
    private Result(T result, boolean issuccess, String rtncode, String rtnmessage) {
        this.result = result;
        this.issuccess = issuccess;
        this.rtncode = rtncode;
        this.rtnmessage = rtnmessage;
    }

    /**
     * 构造函数
     * @param result 响应结果
     * @param rtncode 响应编码
     * @param rtnmessage 响应消息
     */
    private Result(T result, String rtncode, String rtnmessage) {
        this.result = result;
        this.rtncode = rtncode;
        this.rtnmessage = rtnmessage;
    }

    /**
     * 构造函数
     * @param rtncode 响应编码
     * @param rtnmessage 响应消息
     */
    private Result(String rtncode, String rtnmessage) {
        this.rtncode = rtncode;
        this.rtnmessage = rtnmessage;
        this.solution = "请联系服务商!";
    }


    /**
     * 业务请求成功，返回业务代码和描述信息
     */
    public static Result<Void> ok() {
        return new Result<>(ResultEnum.SUCCESS, null);
    }

    /**
     * 业务请求成功，返回业务代码和描述信息
     */
    public static Result<Void> ok(Result Result) {
        return Result;
    }

    /**
     * 业务请求成功，返回业务代码和描述信息，数据
     */
    public static <T> Result<T> ok(T data) {
        return new Result<>(ResultEnum.SUCCESS, data);
    }

    /**
     * 业务请求失败
     */
    public static <T> Result<T> error() {
        return new Result<>(ResultEnum.NORMAL_ERROR, null);
    }

    /**
     * 业务请求失败，返回错误响应码
     */
    public static <T> Result<T> error(ResultEnum ResultEnum) {
        return new Result<>(ResultEnum);
    }

    /**
     * 业务请求失败，返回错误响应码
     */
    public static <T> Result<T> error(ResultEnum ResultEnum, String message, String rtncode) {
        return new Result<>(ResultEnum, message, rtncode);
    }

    /**
     * 业务请求失败
     */
    public static <T> Result<T> error(String message) {
        return new Result<>(ResultEnum.NORMAL_ERROR, message);
    }

    /**
     * 业务请求失败
     */
    public static <T> Result<T> error(String rtncode, String rtnmessage) {
        return new Result<>(rtncode, rtnmessage);
    }

    /**
     * 业务请求失败
     */
    public static <T> Result<T> error(ResultEnum ResultEnum, String rtnmessage) {
        return new Result<>(ResultEnum, rtnmessage);
    }
}
